package serial

import android.content.Context
import rust_serial.NativeSerialPort
import serial.usb.UsbSerialPortMod


interface SerialPort {

    fun read(): ByteArray

    fun write(buf: ByteArray)

    suspend fun close()

    companion object {

        private const val USB_NAME = "USB"

        suspend fun open(context: Context, name: String, baudRate: Int): SerialPort {
            if (name == USB_NAME) {
                return UsbSerialPortMod.open(context, baudRate)
            }
            return NativeSerialPort(name, baudRate)
        }
    }
}

